package com.muzi.weshop.view.adapter;

import android.content.Context;
import android.graphics.Typeface;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.muzi.weshop.R;
import com.muzi.weshop.common.LoginManager;
import com.muzi.weshop.common.contants.Constants;
import com.muzi.weshop.model.OrderMsgDtoListBean;

import java.util.ArrayList;
import java.util.List;

public class OrderListAdapter extends BaseQuickAdapter<OrderMsgDtoListBean, BaseViewHolder> {
    private Context context;
    private OrderImgAdapter orderImgAdapter;
    private List<String> imgUrlList;

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public OrderListAdapter(int layoutResId, @Nullable List<OrderMsgDtoListBean> data, Context context) {
        super(layoutResId, data);
        this.context = context.getApplicationContext();
    }

    @Override
    protected void convert(BaseViewHolder helper, OrderMsgDtoListBean item) {
        TextView tvReceive = helper.itemView.findViewById(R.id.tvReceive);
        TextView tvCancelOrder = helper.itemView.findViewById(R.id.tvCancelOrder);



        //商家才可接单
        if(!LoginManager.getInstance().getPersonal().getLink().equals("0")){
            //只有未接单才显示接单按钮，已接单则不显示接单按钮
            if(item.getStep().equals("0")){
                tvReceive.setVisibility(View.VISIBLE);
            }
            tvCancelOrder.setVisibility(View.GONE);
        }else{
            tvReceive.setVisibility(View.GONE);
            tvCancelOrder.setVisibility(View.VISIBLE);
        }


        //订单状态 0：下单 1：已结单 2 ：已完成 3:已取消
        switch (item.getStep()){
            case "0":
                //下单
                helper.setText(R.id.tvOrderStatus , "未接单");
                if(!LoginManager.getInstance().getPersonal().getLink().equals("0")){

                    tvReceive.setVisibility(View.VISIBLE);
                }else{
                    tvReceive.setVisibility(View.GONE);
                }

                tvCancelOrder.setVisibility(View.VISIBLE);

                break;
            case "1":
                //已接单
                helper.setText(R.id.tvOrderStatus , "已接单");
                tvCancelOrder.setVisibility(View.GONE);
                tvReceive.setVisibility(View.GONE);
                break;
            case "2":
                //已完成
                helper.setText(R.id.tvOrderStatus , "已完成");
                tvReceive.setVisibility(View.GONE);
                tvCancelOrder.setVisibility(View.GONE);
                break;
            case "3":
                //已取消
                helper.setText(R.id.tvOrderStatus , "用户已取消");
                tvReceive.setVisibility(View.GONE);
                tvCancelOrder.setVisibility(View.GONE);
                break;
            default:
                break;
        }

        Typeface iconfont = Typeface.createFromAsset(context.getAssets(), "iconfont/iconfont.ttf");
        TextView tvShopName = helper.itemView.findViewById(R.id.tvShopName);
        tvShopName.setTypeface(iconfont);
        RecyclerView rvOrderImg = helper.itemView.findViewById(R.id.rvOrderImg);
        rvOrderImg.setLayoutManager(new LinearLayoutManager(context, RecyclerView.HORIZONTAL, false));
        imgUrlList = new ArrayList<>();
        orderImgAdapter = new OrderImgAdapter(R.layout.item_order_img, imgUrlList);
        rvOrderImg.setAdapter(orderImgAdapter);

        try {
            if (item.getImg() != null) {
                String[] imgs = item.getImg().split(",");
                if (imgs != null) {
                    for (String img : imgs) {
                        StringBuffer urlBuffer = new StringBuffer();
                        urlBuffer.append(Constants.IMAGE_BASE_URL).append(img);
                        imgUrlList.add(urlBuffer.toString());
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        orderImgAdapter.setNewData(imgUrlList);


        //点击查看地址的时候
        helper.itemView.findViewById(R.id.tvSeeInfo).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onItemClickListener != null){
                    onItemClickListener.onAddressClick(item);
                }
            }
        });

        //点击删除的时候
        tvCancelOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onItemClickListener != null){
                    onItemClickListener.onCancel(item.getId());
                }
            }
        });


        tvReceive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onItemClickListener != null){
                    onItemClickListener.onReceiveClick(item.getId());
                }
            }
        });
    }


    public interface OnItemClickListener{
        void onAddressClick(OrderMsgDtoListBean info);

        void onCancel(int id);

        void onReceiveClick(int id);
    }
}
